package com.sanhe.a448.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.github.gzuliyujiang.wheelpicker.DatePicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
import com.menu.app.MyApp;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.model.Models;
import com.menu.app.ui.SwitchButton;
import com.menu.app.util.Constant;
import com.menu.app.util.Utils;
import com.plata.base.aclr.utils.SPHelper;
import com.sanhe.a448.databinding.ActivitySetBinding;

import java.util.Calendar;
import java.util.Locale;

public class SetActivity extends MenuBaseActivity implements OnDatePickedListener {

    ActivitySetBinding binding;

    @Override
    protected boolean enableMenu() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySetBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.back.setOnClickListener(v -> finish());

        binding.msn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String now = s.toString();
                String big = now.toUpperCase(Locale.US);
                if (!big.equals(now)) {
                    binding.msn.setText(big);
                    binding.msn.setSelection(big.length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        binding.msn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    binding.msn.selectAll();
                }
            }
        });
        binding.msn.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {
                    Utils.hideInput(arg0);
                    String s = binding.msn.getText().toString().trim();
                    if (!TextUtils.isEmpty(s)) {
                        Utils.setMsn(s);
                    }
                    return true;
                }
                return false;
            }
        });

        binding.rent.setOnClickListener(v -> {
            showPicker();
        });

        binding.check.setOnClickListener(v -> {
            int i = Models.Companion.get().getRentEvent().getValue();
            if (i == 1) {
                i = 0;
                SPHelper.getInstance(MyApp.getApp()).removeAndCommit("end_time");
                SPHelper.getInstance(this).putBoolean("rent", false).commit();
            } else {
                i = 1;
                Calendar c = Calendar.getInstance();
                int y = c.get(Calendar.YEAR);
                int m = c.get(Calendar.MONTH);
                if (m == 11) {
                    y += 1;
                    m = 0;
                } else {
                    m += 1;
                }
                c.set(Calendar.YEAR, y);
                c.set(Calendar.MONTH, m);
                SPHelper.getInstance(MyApp.getApp()).putLong("end_time", c.getTimeInMillis()).apply();
                SPHelper.getInstance(this).putBoolean("rent", true).commit();
            }
            Models.Companion.get().getRentEvent().postValue(i);
            showRent();
        });

        boolean bo = Constant.hasLightCtrl();
        if (bo) {
            binding.lightSwitch.openButton();
        } else {
            binding.lightSwitch.closeButton();
        }
        binding.lightSwitch.setOnSwitchListener(new SwitchButton.OnSwitchListener() {
            @Override
            public void onSwitchChange(View view, boolean z2) {
                Constant.setLightCtrl(z2);
            }
        });
    }

    private void showPicker() {
        DatePicker picker = new DatePicker(this);
//picker.setBodyWidth(240);
        DateWheelLayout wheelLayout = picker.getWheelLayout();
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
        wheelLayout.setDateLabel(getString(com.menu.app.R.string.year), getString(com.menu.app.R.string.month), "");
//        wheelLayout.setDateFormatter(new UnitDateFormatter());
        DateEntity tmp = DateEntity.today();
        int y = tmp.getYear();
        int m = tmp.getMonth();
        if (m == 12) {
            y += 1;
            m = 1;
        } else {
            m += 1;
        }
        int d = tmp.getDay();
        wheelLayout.setRange(DateEntity.today(), DateEntity.target(y + 10, 12, 31), DateEntity.target(y, m, d));
        wheelLayout.setCurtainEnabled(true);
        wheelLayout.setCurtainColor(Color.LTGRAY);
        wheelLayout.setIndicatorEnabled(true);
        wheelLayout.setIndicatorColor(Color.DKGRAY);
        wheelLayout.setIndicatorSize(getResources().getDisplayMetrics().density * 2);
        wheelLayout.setTextColor(Color.BLACK);
        wheelLayout.setTextSize(25);
        wheelLayout.setSelectedTextSize(30);
//        wheelLayout.setSelectedTextColor(0xFFFF0000);
//        wheelLayout.getYearLabelView().setTextColor(0xFF999999);
//        wheelLayout.getMonthLabelView().setTextColor(0xFF999999);
        picker.getWheelLayout().setResetWhenLinkage(false);
        picker.setOnDatePickedListener(this);
        picker.show();
    }

    private void showRent() {
        long time = SPHelper.getInstance(MyApp.getApp()).getLong("end_time", 0);
        showRent(time);
    }

    private void showRent(final long time) {
        if (time > 0) {
            binding.rent.setEnabled(true);
            binding.check.setChecked(true);
            binding.rent.setText(Utils.getDay4(time));
        } else {
            binding.rent.setEnabled(false);
            binding.check.setChecked(false);
            binding.rent.setText("0");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.msn.setText(Utils.getMsn());
        showRent();
    }

    @Override
    protected byte[][] getInterest() {
        return new byte[0][];
    }

    @Override
    protected void updateUI() {

    }

    @Override
    public void exit() {

    }

    @Override
    public String getPageIndex() {
        return null;
    }

    long endTime;

    @Override
    public void onDatePicked(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);
        endTime = c.getTimeInMillis();
        SPHelper.getInstance(MyApp.getApp()).putLong("end_time", c.getTimeInMillis()).apply();
        showRent(endTime);
    }
}
